#include <stdio.h>
#include <unistd.h>
#include <signal.h>
#include <time.h>
struct sigaction sa_new ,sa_old;
void mySigHandler(int iSysSignalNumber)
{
    if (iSysSignalNumber == SIGTERM)
    {
        printf(" receive signal SIGTERM:%d default will call exit(0),here we do nothing,then app will continue running...%d\n",iSysSignalNumber,(int)time(NULL));
        sleep(10);
        printf("signal SIGTERM  execute my own code over spent 10 seconds, now continue to execute the old handler\n");
        sigaction(SIGTERM,&sa_old, NULL);
        raise(SIGTERM);
        return;
    }
    if (iSysSignalNumber == SIGQUIT)
    {
        printf(" receive signal SIGQUIT:%d default will call exit(0),here we do nothing,then app will continue running...\n",iSysSignalNumber);
        
        return;
    }
    
}
int main(int argc,const char* argv[])
{
    // signal(SIGTERM,mySigHandler); // 简单处理信号可以这样写。
    // signal(SIGQUIT,mySigHandler);

    int ignoredSigs[] = {3,4,5};     // 在被捕获的信号处理期间，忽律某些特定的信号，其中SIGKILL/SIGSTOP是无法被捕获，也是无法被忽略的
    sa_new.sa_handler = mySigHandler;
    sa_new.sa_flags = 0;
    sigemptyset(&sa_new.sa_mask);
    for(int i=0;i<sizeof(ignoredSigs);i++)
    {
        sigaddset(&sa_new.sa_mask,ignoredSigs[i]);
    }
    sigaction(SIGTERM,&sa_new, &sa_old); // 需要处理信号的更为详细的信息，需要这样写。

    int icnt = 0;
    while(1)
    {
        printf("app is running...%d\n",icnt++);
        sleep(3);
    }
    return 0;
}